package com.ruoyi.resume.service.impl;

import com.ruoyi.resume.domain.*;
import com.ruoyi.resume.dto.ResumeDTO;
import com.ruoyi.resume.mapper.*;
import com.ruoyi.resume.service.IResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class IResumeServiceImpl implements IResumeService {
    @Autowired
    private ResumeMapper resumeMapper;
    @Autowired
    private BoleResumeWorkMapper boleResumeWorkMapper;
    @Autowired
    private BoleResumeProjectMapper boleResumeProjectMapper;
    @Autowired
    private BoleResumeHonorMapper boleResumeHonorMapper;
    @Autowired
    private BoleResumeEducationMapper boleResumeEducationMapper;
    @Autowired
    private BoleResumeSkillsMapper boleResumeSkillsMapper;
    @Override
    public ResumeDTO getResume(Long userId) {
        //拿到主表简历信息后 再去查相关信息         这里是可以优化的
        ResumeDTO resumeDTO = resumeMapper.getResume(userId);
        if (resumeDTO==null){
            return null;
        }
        //查询信息
        BoleResumeWork boleResumeWork = new BoleResumeWork();
        boleResumeWork.setResumeId(resumeDTO.getId());

        BoleResumeProject  boleResumeProject = new BoleResumeProject();
        boleResumeProject.setResumeId(resumeDTO.getId());

        BoleResumeHonor boleResumeHonor = new BoleResumeHonor();
        boleResumeHonor.setResumeId(resumeDTO.getId());

        BoleResumeEducation boleResumeEducation = new BoleResumeEducation();
        boleResumeEducation.setResumeId(resumeDTO.getId());

        BoleResumeSkills boleResumeSkills = new BoleResumeSkills();
        boleResumeSkills.setResumeId(resumeDTO.getId());

        resumeDTO.setHonors(boleResumeHonorMapper.selectBoleResumeHonorList(boleResumeHonor));
        resumeDTO.setWorkExperiences(boleResumeWorkMapper.selectBoleResumeWorkList(boleResumeWork));
        resumeDTO.setProjects(boleResumeProjectMapper.selectBoleResumeProjectList(boleResumeProject));
        resumeDTO.setEducations(boleResumeEducationMapper.selectBoleResumeEducationList(boleResumeEducation));
        resumeDTO.setSkills(boleResumeSkillsMapper.selectBoleResumeSkillsList(boleResumeSkills));
        return resumeDTO;
    }

    @Transactional
    @Override
    public void insertResumeWithChildren(ResumeDTO resumeDTO) {
//        resumeDTO.
        resumeMapper.insertResume(resumeDTO);
        Long resumeId = resumeDTO.getId();
        System.out.println(resumeId);

        List<BoleResumeEducation> educations = resumeDTO.getEducations();
        if (educations != null && !educations.isEmpty()) {
            for (com.ruoyi.resume.domain.BoleResumeEducation education : educations) {
                education.setResumeId(resumeId);
            }
            resumeMapper.insertEducations(educations);
        }

        List<com.ruoyi.resume.domain.BoleResumeWork> workExperiences = resumeDTO.getWorkExperiences();
        if (workExperiences != null && !workExperiences.isEmpty()) {
            for (com.ruoyi.resume.domain.BoleResumeWork work : workExperiences) {
                work.setResumeId(resumeId);
            }
            resumeMapper.insertWorkExperiences(workExperiences);
        }

        List<com.ruoyi.resume.domain.BoleResumeProject> projects = resumeDTO.getProjects();
        if (projects != null && !projects.isEmpty()) {
            for (com.ruoyi.resume.domain.BoleResumeProject project : projects) {
                project.setResumeId(resumeId);
            }
            resumeMapper.insertProjects(projects);
        }

        List<com.ruoyi.resume.domain.BoleResumeHonor> honors = resumeDTO.getHonors();
        if (honors != null && !honors.isEmpty()) {
            for (com.ruoyi.resume.domain.BoleResumeHonor honor : honors) {
                honor.setResumeId(resumeId);
            }
            resumeMapper.insertHonors(honors);
        }

        List<com.ruoyi.resume.domain.BoleResumeSkills> skills = resumeDTO.getSkills();
        if (skills != null && !skills.isEmpty()) {
            for (com.ruoyi.resume.domain.BoleResumeSkills skill : skills) {
                skill.setResumeId(resumeId);
            }
            resumeMapper.insertSkills(skills);
        }
    }

    //修改
    // 修改简历信息及关联信息
    @Transactional
    @Override
    public void updateResumeWithChildren(ResumeDTO resumeDTO) {
        Long resumeId = resumeDTO.getId();
        // 更新主表信息
        resumeMapper.updateResume(resumeDTO);

        // 先删除原有的子表关联数据
        resumeMapper.deleteEducationsByResumeId(resumeId);
        resumeMapper.deleteWorkExperiencesByResumeId(resumeId);
        resumeMapper.deleteProjectsByResumeId(resumeId);
        resumeMapper.deleteHonorsByResumeId(resumeId);
        resumeMapper.deleteSkillsByResumeId(resumeId);
        // 再插入新的子表关联数据
        List<com.ruoyi.resume.domain.BoleResumeEducation> educations = resumeDTO.getEducations();
        if (educations != null && !educations.isEmpty()) {
            for (com.ruoyi.resume.domain.BoleResumeEducation education : educations) {
                education.setResumeId(resumeId);
            }
            resumeMapper.insertEducations(educations);
        }

        List<com.ruoyi.resume.domain.BoleResumeWork> workExperiences = resumeDTO.getWorkExperiences();
        if (workExperiences != null && !workExperiences.isEmpty()) {
            for (com.ruoyi.resume.domain.BoleResumeWork work : workExperiences) {
                work.setResumeId(resumeId);
            }
            resumeMapper.insertWorkExperiences(workExperiences);
        }

        List<com.ruoyi.resume.domain.BoleResumeProject> projects = resumeDTO.getProjects();
        if (projects != null && !projects.isEmpty()) {
            for (com.ruoyi.resume.domain.BoleResumeProject project : projects) {
                project.setResumeId(resumeId);
            }
            resumeMapper.insertProjects(projects);
        }

        List<com.ruoyi.resume.domain.BoleResumeHonor> honors = resumeDTO.getHonors();
        if (honors != null && !honors.isEmpty()) {
            for (com.ruoyi.resume.domain.BoleResumeHonor honor : honors) {
                honor.setResumeId(resumeId);
            }
            resumeMapper.insertHonors(honors);
        }

        List<com.ruoyi.resume.domain.BoleResumeSkills> skills = resumeDTO.getSkills();
        if (skills != null && !skills.isEmpty()) {
            for (com.ruoyi.resume.domain.BoleResumeSkills skill : skills) {
                skill.setResumeId(resumeId);
            }
            resumeMapper.insertSkills(skills);
        }
    }
}
